<?php

namespace App\Http\Controllers;

use App\Common\LogUtil;
use App\Common\Models\Db1\MpApp;
use App\Common\Models\Db1\MpCallbackContent;
use App\Common\Util;
use App\Common\WxUtil;
use Illuminate\Http\Request;
use EasyWeChat\Factory;
use EasyWeChat;
use Log;

class WeixinController extends Controller
{
    //
    public function index()
    {
        $s = file_get_contents('php://input');
        if (empty($s)) {
            return 'content is empty';
        }

        //记录提交信息
        $model = new MpCallbackContent();
        $model->content = $s;
        $model->param_get = Util::json_encode_unicode($_GET);
        $model->save();

        try {
            $data = simplexml_load_string($s, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS);
            $data = Util::xml2arr($data);
            if (!empty($data['ToUserName'])) {
                $item_app = (new MpApp())->findOne(['origin_id' => $data['ToUserName']]);
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }

        if (empty($item_app)) {
            $errmsg = '关联app未找到';
            //return $errmsg;
            LogUtil::info('weixin', [$data, $errmsg], __FILE__, __LINE__);
            return 'success';
        }
        $config = [
            'app_id' => $item_app->app_id,
            'secret' => $item_app->app_secret,
            'token' => $item_app->token,
            'aes_key' => $item_app->encoding_aes_key,

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
        ];

        try {
            $app = Factory::officialAccount($config); // 公众号
            /*
               $work = EasyWeChat::work(); // 企业微信
               $payment = EasyWeChat::payment(); // 微信支付
               $openPlatform = EasyWeChat::openPlatform(); // 开放平台
               $miniProgram = EasyWeChat::miniProgram(); // 小程序
             */
            $app->server->push(function ($message) {
                if ($message['MsgType'] == 'event') {
                    WxUtil::event($message);
                    return 'success';
                }
                if ($message['MsgType'] == 'text') {
                    WxUtil::text($message);
                    return '您好！欢迎使用';
                }

                return "您好！欢迎使用";
            });
            $response = $app->server->serve();
            return $response;
        } catch (\Exception $e) {
            return $e->getMessage() . "\n";
        }
        return 'success';
    }

    public function notify()
    {
        $s = file_get_contents('php://input');
        if (empty($s)) {
            return 'success';
        }

        //记录提交信息
        $model = new MpCallbackContent();
        $model->content = $s;
        $model->param_get = Util::json_encode_unicode($_GET);
        $model->save();

        return 'success';

        try {
            $data = simplexml_load_string($s, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS);
            $data = Util::xml2arr($data);
            if (!empty($data['ToUserName'])) {
                $item_app = (new MpApp())->findOne(['origin_id' => $data['ToUserName']]);
            }
        } catch (\Exception $e) {
            return $e->getMessage();
        }

        if (empty($item_app)) {
            $errmsg = '关联app未找到';
            //return $errmsg;
            LogUtil::info('weixin', [$data, $errmsg], __FILE__, __LINE__);
            return 'success';
        }
        $config = [
            'app_id' => $item_app->app_id,
            'secret' => $item_app->app_secret,
            'token' => $item_app->token,
            'aes_key' => $item_app->encoding_aes_key,

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
        ];

        try {
            $app = Factory::officialAccount($config); // 公众号
            /*
               $work = EasyWeChat::work(); // 企业微信
               $payment = EasyWeChat::payment(); // 微信支付
               $openPlatform = EasyWeChat::openPlatform(); // 开放平台
               $miniProgram = EasyWeChat::miniProgram(); // 小程序
             */
            $app->server->push(function ($message) {
                if ($message['MsgType'] == 'event') {
                    WxUtil::event($message);
                    return 'success';
                }
                if ($message['MsgType'] == 'text') {
                    WxUtil::text($message);
                    return '您好！欢迎使用';
                }

                return "您好！欢迎使用";
            });
            $response = $app->server->serve();
            return $response;
        } catch (\Exception $e) {
            return $e->getMessage() . "\n";
        }
        return 'success';
    }
}
